package com.ruoyi.process.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.process.domain.ProcessSellrecord;
import com.ruoyi.process.mapper.ProcessSellrecordMapper;
import com.ruoyi.wms.mapper.WmsProductstockMapper;
import com.ruoyi.wms.service.IWmsProductstockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.process.mapper.ProcessOrderMapper;
import com.ruoyi.process.domain.ProcessOrder;
import com.ruoyi.process.service.IProcessOrderService;
import com.ruoyi.common.core.text.Convert;

/**
 * 生产订单Service业务层处理
 * 
 * @author chao
 * @date 2024-07-25
 */
@Service
public class ProcessOrderServiceImpl implements IProcessOrderService 
{
    @Autowired
    private ProcessOrderMapper processOrderMapper;

    @Autowired
    private WmsProductstockMapper wmsProductstockMapper;

    @Autowired
    private ProcessSellrecordMapper processSellrecordMapper;

    /**
     * 查询生产订单
     * 
     * @param id 生产订单主键
     * @return 生产订单
     */
    @Override
    public ProcessOrder selectProcessOrderById(Long id)
    {
        return processOrderMapper.selectProcessOrderById(id);
    }

    /**
     * 查询生产订单列表
     * 
     * @param processOrder 生产订单
     * @return 生产订单
     */
    @Override
    public List<ProcessOrder> selectProcessOrderList(ProcessOrder processOrder)
    {
        return processOrderMapper.selectProcessOrderList(processOrder);
    }

    /**
     * 新增生产订单
     * 
     * @param processOrder 生产订单
     * @return 结果
     */
    @Override
    public int insertProcessOrder(ProcessOrder processOrder)
    {
        return processOrderMapper.insertProcessOrder(processOrder);
    }

    /**
     * 修改生产订单
     * 
     * @param processOrder 生产订单
     * @return 结果
     */
    @Override
    public int updateProcessOrder(ProcessOrder processOrder)
    {
        return processOrderMapper.updateProcessOrder(processOrder);
    }

    /**
     * 批量删除生产订单
     * 
     * @param ids 需要删除的生产订单主键
     * @return 结果
     */
    @Override
    public int deleteProcessOrderByIds(String ids)
    {
        return processOrderMapper.deleteProcessOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除生产订单信息
     * 
     * @param id 生产订单主键
     * @return 结果
     */
    @Override
    public int deleteProcessOrderById(Long id)
    {
        return processOrderMapper.deleteProcessOrderById(id);
    }

    @Override
    public int excuteOutStorge(ProcessOrder processOrder) {

        int stock = wmsProductstockMapper.selectStockByProductId(processOrder.getProductId(),processOrder.getNum());

        if(stock < 0){
            return -1;
        }
        else{

            ProcessSellrecord processSellrecord = new ProcessSellrecord();
            processSellrecord.setCode(processOrder.getOrderCode());
            processSellrecord.setClientName(processOrder.getClientName());
            processSellrecord.setClientPhone(processOrder.getClientPhone());
            processSellrecord.setProductName(processOrder.getProductName());
            processSellrecord.setProductModel(processOrder.getModel());
            processSellrecord.setNum(processOrder.getNum());
            processSellrecord.setUnitName(processOrder.getProductUnitname());
            processSellrecord.setSignalPrice(processOrder.getSignalPrice());
            processSellrecord.setTotalPrice(processOrder.getTotalPrice());
            processSellrecord.setPay(processOrder.getPay());
            processSellrecord.setCreatetime(new Date());

            processSellrecordMapper.insertProcessSellrecord(processSellrecord);

            processOrderMapper.updateSellStatus(processOrder.getId());
            return wmsProductstockMapper.excuteOutStorge(processOrder.getProductId(),processOrder.getNum());
        }

    }
}
